<?php

namespace core;

class Log 
{
    private static $logRootPath = '../runtime/';

    private static $logPath = 'logs/';

    /**
     * 记录模块访问日志
     */
    public static function saveModuleLog($moduleName, $controllerName, $content)
    {
        // 每天生成一个新文件
        $fileName = $controllerName . '_' . date('Y_m_d') . '.log';

        if (!file_exists(self::$logRootPath . self::$logPath . $moduleName)) {
            $mkRes = mkdir(self::$logRootPath . self::$logPath . $moduleName, 0777, true);
            if (!$mkRes) {
                return '没有相对应的文件夹，并且创建失败';
            }
        }

        $logFile = self::$logRootPath . self::$logPath . $moduleName . '/' . $fileName;
        self::write($logFile, $content);
    }

    /**
     * 写文件的公用方法，传递文件名称和内容作为参数
     */
    public static function write($logFile, $content = '')
    {
        $logFileFd = fopen($logFile, "a");
        fwrite($logFileFd, $content . "\r\n");
        fclose($logFileFd);
    }
}